#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QListWidget>
#include<QListWidgetItem>
#include<QDebug>
#include<QComboBox>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QListWidget *pListWidget = new QListWidget(this);
    int i = 0;
    do {
        ++i;
        QListWidgetItem *pItem = new QListWidgetItem(pListWidget);
        User user;
        user.nID = i;
        user.strName = QString("Qter %1").arg(i);
        user.sex=i%2==0?"男":"女";
        pItem->setData(Qt::UserRole, QVariant::fromValue(user));  // 设置用户数据
        pItem->setText(QString("Item %1").arg(i));
        pListWidget->addItem(pItem);
    } while (i < 5);

    // 连接信号槽
    connect(pListWidget, &QListWidget::itemClicked, this, &MainWindow::onItemClicked);
    QComboBox *pComboBox = new QComboBox(this);
    pComboBox->addItem("Chinese");
    pComboBox->addItem("English");
    pComboBox->setItemData(0, QVariant::fromValue(UI_ZH));  // 设置用户数据
    pComboBox->setItemData(1, QVariant::fromValue(UI_EN));  // 设置用户数据

    // 连接信号槽
    connect(pComboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &MainWindow::onCurrentIndexChanged);
    ui->gridLayout->addWidget(pListWidget);
    ui->gridLayout->addWidget(pComboBox);
}
void MainWindow::onCurrentIndexChanged(int index) {
    // 获取发送者
    QObject *pObject = this->sender();
    QComboBox *pComboBox = qobject_cast<QComboBox *>(pObject);

    // 获取用户数据
    QVariant variant = pComboBox->itemData(index);
    LANGUAGE language = variant.value<LANGUAGE>();

    qDebug() << "Language : " << language;
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::onItemClicked(QListWidgetItem *item) {
    QVariant variant = item->data(Qt::UserRole);  // 获取用户数据
    User user = variant.value<User>();

    qDebug() << "ID : " << user.nID;
    qDebug() << "Name : " << user.strName;
    qDebug()<<"Set: "<<user.sex;
}
